const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin'); //通过 npm 安装
const webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
let pageOptios = {
    template: './src/index.html',
    filename: "index.html",
    hash: true,
    cache: true,
    chunks:'async',
    minify: {
        collapseWhitespace: true,//是否去除html中的空格、换行符,元素内的不会去除的
        removeComments: true,//是否去除html注释
        minifyCSS: true,// 压缩内联css
        removeRedundantAttributes: true,//
        removeScriptTypeAttributes: true,//
        removeStyleLinkTypeAttributes: true,//
        useShortDoctype: true//
    },
    thunks:['']
}
module.exports = {
    mode: "development",
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "style-loader" // 将 JS 字符串生成为 style 节点
                    },
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader", // 将 CSS 转化成 CommonJS 模块

                    },
                    {
                        loader: 'postcss-loader',
                        options: {}
                    },

                    {
                        loader: "sass-loader" // 将 Sass 编译成 CSS
                    }, {
                        loader: "sass-resources-loader",
                        options: {
                            resources: ['./index.scss']
                        }
                    }
                ]

            },
            {
                test: /\.js$/,
                use: 'babel-loader'
            },
            {
                test: /\.ts$/,
                use: 'ts-loader'
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    // 需要下载file-loader和url-loader
                    loader: "url-loader",
                    options: {
                        limit: 5 * 1024,//小于这个时将会已base64位图片打包处理
                        // 图片文件输出的文件夹
                        outputPath: "images"
                    }
                }]
            }
        ]
    },
    entry: {
        Home: './src/pages/Home/Home.ts',
        Login: './src/pages/Login/Login.ts',
        Index: "./src/index.ts"
    },
    output: {
        path: path.join(__dirname, './dist'),
        filename: '[name].[hash].js',
    },
    optimization: {
        minimize: true,
        minimizer: [
            new TerserPlugin({
                cache: true,
            }),
        ],
    },
    devServer: {
        historyApiFallback: true,
        inline: true,//实时刷新
        hot: true,
        open: true,
        port: 8080,
    },
    watchOptions: {
        poll: 1000,//监测修改的时间(ms)
        aggregateTimeout: 500,//防止重复按键，500毫秒内算按一次
        ignored: /node_modules/,//不监测
    },
    plugins: [
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: "css/[name].[hash].css",////都提到build目录下的css目录中
            chunkFilename: "[id].css"
        }),
        new OptimizeCSSAssetsPlugin(),
        new HtmlWebpackPlugin({...pageOptios, chunks: ['Index']}),
        new HtmlWebpackPlugin({...pageOptios, chunks: ['Home'], filename: "Home.html", template: "./src/pages/Home/Home.html",

        }),
        new HtmlWebpackPlugin({...pageOptios,chunks: ['Login'], filename: "Login.html", template: "./src/pages/Login/Login.html",

        }),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ],
    resolve: {
        extensions: ['.ts', '.tsx', '.js']
    },
};
